#include "login.h"
#include "ui_login.h"
#include <QGraphicsDropShadowEffect>
#include <QRegularExpressionValidator>
#include "model/datacenter.h"
#include "toast.h"
#include "util.h"
Login::Login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
{
    ui->setupUi(this);

    // 去掉边框, 设置窗口为对话框
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
    // 设置为窗口模态
    this->setWindowModality(Qt::WindowModal);

    // 添加阴影效果
    this->setAttribute(Qt::WA_TranslucentBackground);   // 窗口必须透明
    QGraphicsDropShadowEffect* showEffect = new QGraphicsDropShadowEffect(this);
    showEffect->setColor(Qt::black);
    showEffect->setBlurRadius(5);
    showEffect->setOffset(0);
    ui->loginBg->setGraphicsEffect(showEffect);

    // 默认为手机号登录
    ui->messageWidget->show();
    ui->passwordWidget->hide();

    // 手机号和账号编辑框限制
    QRegularExpression regExp("^1\\d{10}$");
    QValidator* validator = new QRegularExpressionValidator(regExp, this);
    ui->accountNum->setValidator(validator);
    ui->phoneNum->setValidator(validator);

    // 对于LineEdit进行限制
    ui->passwordNum->setEchoMode(QLineEdit::Password);

    // 创建验证码验证器
    QIntValidator* inValidator = new QIntValidator(0,  999999, ui->authcode);
    ui->authcode->setValidator(inValidator);
    ui->authcode->setMaxLength(6);

    // 连接信号槽
    connect(ui->min, &QPushButton::clicked, this, &QWidget::showMinimized);
    connect(ui->quit, &QPushButton::clicked, this, &QWidget::close);
    connect(ui->messageBtn, &QPushButton::clicked, this, &Login::onMessageBtnClicked);
    connect(ui->passwordBtn, &QPushButton::clicked, this, &Login::onPasswordBtnClicked);
    connect(ui->authcodeBtn, &QPushButton::clicked, this, &Login::onAuthcodeBtnClicked);
    // 登录注册按钮
    connect(ui->loginOrRegister, &QPushButton::clicked, this, &Login::onLoginAndRegisterBtnClicked);
    // 立即注册注册按钮点击
    connect(ui->registerNow, &QPushButton::clicked, this, &Login::onRegisterNowBtnClicked);
    // 登录按钮点击
    connect(ui->loginNow, &QPushButton::clicked, this, &Login::onLoginNowBtnClicked);


    // 获取短信验证码成功
    auto dataCenter = model::DataCenter::getInstance();
    connect(dataCenter, &model::DataCenter::getAuthcodeDone, this, &Login::onAuthcodeDone);

    // 短信登录成功
    connect(dataCenter, &model::DataCenter::loginWithMessageDone, this, [=](){
        emit loginSuccess();  // 由MySelfWidget来处理，从新获取登录成功的用户的个人信息 以及 视频列表
        close();
    });

    // 短信登录失败
    connect(dataCenter, &model::DataCenter::loginWithMessageFailed, this, [=](const QString& errorInfo){
        Toast::showMessage(errorInfo);
    });

    // 密码登录成功s
    connect(dataCenter, &model::DataCenter::loginWithPasswordDone, this, [=](){
        emit loginSuccess();  // 由MySelfWidget来处理，从新获取登录成功的用户的个人信息 以及 视频列表
        close();
    });

    // 密码登录失败
    connect(dataCenter, &model::DataCenter::loginWithPasswordFailed, this, [=](const QString& errorInfo){
        Toast::showMessage(errorInfo);
    });
}

Login::~Login()
{
    delete ui;
}

void Login::reset()
{
    ui->accountNum->setText("");
    ui->passwordNum->setText("");
    ui->phoneNum->setText("");
    ui->authcode->setText("");
}

void Login::onPasswordBtnClicked()
{
    ui->messageWidget->hide();
    ui->passwordWidget->show();
    ui->passwordBtn->setStyleSheet("QPushButton{"
                                   "color : #3ECEFE;"
                                   "font-weight:bold;"
                                   "border : none;"
                                   "border-bottom : 6px solid #3ECEFE;}");
    ui->messageBtn->setStyleSheet("QPushButton{"
                                  "color : #222222;"
                                  "border : none;"
                                  "border-bottom: 2px solid #B5ECFF;}");

}

void Login::onMessageBtnClicked()
{
    ui->messageWidget->show();
    ui->passwordWidget->hide();
    ui->messageBtn->setStyleSheet("QPushButton{"
                                  "color : #3ECEFE;"
                                  "font-weight:bold;"
                                  "border : none;"
                                  "border-bottom : 6px solid #3ECEFE;}");
    ui->passwordBtn->setStyleSheet("QPushButton{"
                                   "color : #222222;"
                                   "border : none;"
                                   "border-bottom: 2px solid #B5ECFF;}");
}

void Login::onAuthcodeBtnClicked()
{
    // 获取手机号
    QString phoneNum = ui->phoneNum->text();
    if(phoneNum.isEmpty()){
        Toast::showMessage("请输入手机号!!!");
    }else{
        auto dataCenter = model::DataCenter::getInstance();
        dataCenter->getAuthcodeAsync(phoneNum);
    }
}

void Login::onAuthcodeDone(const QString& authcodeId)
{
    this->authcodeId = authcodeId;
    LOG() << "获取短信验证码成功，短信验证码的id为："<<authcodeId;
}

void Login::onLoginAndRegisterBtnClicked()
{
    // 检测手机号 或 验证码是否输入
    const QString phoneNum = ui->phoneNum->text();
    const QString authcode = ui->authcode->text();
    if(phoneNum.isEmpty() || authcode.isEmpty()){
        Toast::showMessage("手机号 或 验证码 不能为空!!!");
        return;
    }
    // 发送短信登录请求
    auto dataCenter = model::DataCenter::getInstance();
    dataCenter->loginWithMessageAsync(phoneNum, authcode, authcodeId);
}

void Login::onRegisterNowBtnClicked()
{
    ui->registerNow->setStyleSheet("#registerNow{"
                                   "color : #FFFFFF;"
                                   "background-color : #3ECEFE;"
                                   "border-radius : 36px;}");
    ui->loginNow->setStyleSheet("#loginNow{"
                                "color : #222222;"
                                "background-color : #FFFFFF;"
                                "border-radius : 36px;"
                                "border: 1px solid #C2C9D9;}");
    // 切换到短信登录⻚⾯
    onMessageBtnClicked();
}

void Login::onLoginNowBtnClicked()
{
    // 切换 立即注册 和 立即登录 按钮的样式
    ui->loginNow->setStyleSheet("#loginNow{"
                                "background-color : #3ECEFE;"
                                "color : #FFFFFF;"
                                "border-radius : 36px;"
                                "}");
    ui->registerNow->setStyleSheet("#registerNow{"
                                   "background-color : #FFFFFF;"
                                   "color : #222222;"
                                   "border-radius : 36px;"
                                   "border : 1px solid, #C2C9D9;}");

    // 获取手机号 和 密码
    QString accountNum = ui->accountNum->text();
    QString password = ui->passwordNum->text();
    // 发送密码登录请求
    auto dataCenter = model::DataCenter::getInstance();
    dataCenter->loginWithPasswordAsync(accountNum, password);
}
